package com.ezops.dto;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.ezops.util.DownloadReport;
import com.ezops.util.Report;

public class DownloadableReport extends AbstractExcelView implements DownloadReport  {
	
	/**
	 * No constructor argument required in Spring in case of parameterized argument is present
	 */
	public DownloadableReport(){
		
	}
	public DownloadableReport(String reportName, Report report) {
		super();
		System.out.println(reportName+"//"+report);
		this.reportName = reportName;
		this.report = report;
	}

	private String reportName;
	private Report report;
	
	@Override
	public String getName() {
		return reportName;
	}

	@Override
	public Report getReport() {
		return report;
	}

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DownloadableReport downloadReport = (DownloadableReport) model.get("report");
		response.setHeader("Content-Disposition", "attachment; filename="+downloadReport.getName());
		Report report = downloadReport.getReport();
		Sheet sheet = workbook.createSheet("Sheet1");
	 	report.save(sheet);
	}

}
